﻿@using Newtouch.Infrastructure;
@{

    ViewBag.Title = "编辑排班时间";
    Layout = "~/Views/Shared/_Form.cshtml";
}

<!DOCTYPE html>

<form id="form1">
    <div style="padding: 20px 35px 0 2px;">
        <table class="form" id="tabletime">
            <tr>
                <th class="formTitle">时间段选择:</th>
                <td>
                    <i id="zdCircle" class="fa fa-plus-circle plusToggleCircle" aria-hidden="true" style="margin-left: 10px; color: #6ff3ad; font-size: large;" onclick="btn_addtime()"></i>
                </td>
            </tr>
            <tr id="trtime1">
                <th class="formTitle">开始时间 ：</th>
                <td class="formValue">
                    <input id="begintime1" type="text" class="form-control input-wdatepicker" value="@DateTime.Now.ToString("HH:mm")" onfocus="WdatePicker({ dateFmt: 'HH:mm' })" />
                </td>
                <th class="formTitle">结束时间 ：</th>
                <td class="formValue">
                    <input id="endtime1" type="text" class="form-control input-wdatepicker" value="@DateTime.Now.ToString("HH:mm")" onfocus="WdatePicker({ dateFmt: 'HH:mm' })" />
                </td>
            </tr>
            </table>
</form>

<script>
    var inttime = 2;
    var sjd = $.request("sjd");
    $(function () {
        $.ajax({
            url: "/OutPatientManage/OutBook/getDateInfosjd",
            dataType: "json",
            async: false,
            success: function (data) {
                for (var i = 0; i < data.length; i++) {
                    if (i == 0) {
                        $('#begintime1').val(data[i].split('-')[0]);
                        $('#endtime1').val(data[i].split('-')[1]);
                    } else {
                    var thisHtml = '<tr id="trtime' + inttime + '"><th class="formTitle"><i class="fa fa-minus minusToggleCircle" aria-hidden="true" style="margin-left: 10px; color: #6ff3ad; font-size: large;"  onclick="btn_clertime(' + inttime + ')"></i>开始时间 ：</th><td class="formValue"><input id="begintime' + inttime + '" type="text" class="form-control input-wdatepicker" value="' + data[i].split('-')[0] + '" onfocus="WdatePicker({ dateFmt: \'HH:mm\' })" /></td><th class="formTitle">结束时间 ：</th><td class="formValue"><input id="endtime' + inttime + '" type="text" class="form-control input-wdatepicker" value="' + data[i].split('-')[1]  + '" onfocus="WdatePicker({ dateFmt: \'HH:mm\' })" /></td></tr>';
                        $('#tabletime').append(thisHtml);
                        inttime += 1;
                    }
                }
            }
        });
        if (sjd != null && sjd != "") {
            if (sjd.split(',').length == 0) {
                $('#begintime1').val(sjd.split('-')[0]);
                $('#endtime1').val(sjd.split('-')[1]);
            } else {
            for (var i = 0; i < sjd.split(',').length; i++) {
                if (i == 0) {
                    $('#begintime1').val(sjd.split('-')[0]);
                    $('#endtime1').val(sjd.split(',')[i].split('-')[1]);
                }
                else {
                    var thisHtml = '<tr id="trtime' + inttime + '"><th class="formTitle"><i class="fa fa-minus minusToggleCircle" aria-hidden="true" style="margin-left: 10px; color: #6ff3ad; font-size: large;"  onclick="btn_clertime(' + inttime + ')"></i>开始时间 ：</th><td class="formValue"><input id="begintime' + inttime + '" type="text" class="form-control input-wdatepicker" value="' + sjd.split(',')[i].split('-')[0]+'" onfocus="WdatePicker({ dateFmt: \'HH:mm\' })" /></td><th class="formTitle">结束时间 ：</th><td class="formValue"><input id="endtime' + inttime + '" type="text" class="form-control input-wdatepicker" value="' + sjd.split(',')[i].split('-')[1]+'" onfocus="WdatePicker({ dateFmt: \'HH:mm\' })" /></td></tr>';
                    $('#tabletime').append(thisHtml);
                    inttime += 1;
                }
                }
            }
        }
    });
    function btn_addtime() {
        var thisHtml = '<tr id="trtime' + inttime + '"><th class="formTitle"><i class="fa fa-minus minusToggleCircle" aria-hidden="true" style="margin-left: 10px; color: #6ff3ad; font-size: large;"  onclick="btn_clertime(' + inttime + ')"></i>开始时间 ：</th><td class="formValue"><input id="begintime' + inttime + '" type="text" class="form-control input-wdatepicker" value="@DateTime.Now.ToString("HH:mm")" onfocus="WdatePicker({ dateFmt: \'HH:mm\' })" /></td><th class="formTitle">结束时间 ：</th><td class="formValue"><input id="endtime' + inttime + '" type="text" class="form-control input-wdatepicker" value="@DateTime.Now.ToString("HH:mm")" onfocus="WdatePicker({ dateFmt: \'HH:mm\' })" /></td></tr>';
        $('#tabletime').append(thisHtml);
        inttime += 1;
    }
    function btn_clertime(inttimetr) {
        $('#trtime' + inttimetr).html('');
    }
    function AcceptClick(callBack) {
        var timestr = "";
        for (var i = 1; i < inttime; i++) {
            if ($('#begintime' + i).val() != null && $('#endtime' + i).val() != null && $('#begintime' + i).val() != "" && $('#endtime' + i).val() != "") {
                if ($('#begintime' + i).val().substring(3, 6) != '00' && $('#begintime' + i).val().substring(3, 6) != '30') {
                    $.modalAlert("开始或结束时间必须以半小时为单位", 'warning');
                    return false;
                }
                if ($('#endtime' + i).val().substring(3, 6) != '00' && $('#endtime' + i).val().substring(3, 6) != '30') {
                    $.modalAlert("开始或结束时间必须以半小时为单位", 'warning');
                    return false;
                }
                if ($('#begintime' + i).val() == $('#endtime' + i).val()) {
                    $.modalAlert("开始时间不能等于结束时间！", 'warning');
                    return false;
                }
                for (var j = 1; j < inttime; j++) {
                    if ($('#begintime' + j).val() != null && $('#endtime' + j).val() != null && $('#begintime' + j).val() != "" && $('#endtime' + j).val() != "") {
                        var begintimej = new Date("2022/06/07 "+$('#begintime' + j).val());
                        var begintimei = new Date("2022/06/07 " +$('#begintime' + i).val());
                        var endtimej = new Date("2022/06/07 " +$('#endtime' + j).val());
                        var endtimi = new Date("2022/06/07 " + $('#endtime' + i).val());
                        
                        if (begintimej == endtimej) {
                            $.modalAlert("结束时间不能等于开始时间", 'warning');
                            return false;
                        }
                        if (begintimei == endtimi) {
                            $.modalAlert("结束时间不能等于开始时间", 'warning');
                            return false;
                        }
                        if (begintimej > endtimej) {
                            $.modalAlert("结束时间必须大于开始时间", 'warning');
                            return false;
                        }
                        if (begintimei > endtimi) {
                            $.modalAlert("结束时间必须大于开始时间", 'warning');
                            return false;
                        }
                        if (begintimej > begintimei && begintimej < endtimi) {
                            $.modalAlert("时间交叉，请查看开始时间是否有误", 'warning');
                            return false;
                        }
                        if (endtimej > begintimei && endtimej < endtimi) {
                            $.modalAlert("时间交叉，请查看结束时间是否有误", 'warning');
                            return false;
                        }
                    }
                }
                timestr += $('#begintime' + i).val() + "-" + $('#endtime' + i).val() + ",";
            }
        }
        timestr = timestr.substring(0, timestr.length - 1);
        $.ajax({
            url: "/OutPatientManage/OutBook/SubmitghpbTime?timestr="+timestr,
            dataType: "json",
            async: false,
            success: function (data) {
                callBack(timestr);
                $.modalClose();
            }
        });
    }
</script>
